package cn.sciento.farm.devicemanager.gateway.uplink.service.insectSituationLamp;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.dao.entity.DevCamera;
import cn.sciento.farm.devicemanager.dao.entity.DevInsectSituationLamp;
import cn.sciento.farm.devicemanager.gateway.model.uplink.InsectLampParam;
import cn.sciento.farm.devicemanager.gateway.model.uplink.InsectLampSwitch;
import cn.sciento.farm.devicemanager.gateway.uplink.service.base.AbstractStringAdapter;
import cn.sciento.farm.devicemanager.sensor.RefreshInverterReq;
import cn.sciento.farm.devicemanager.service.CameraService;
import cn.sciento.farm.devicemanager.service.DevInsectImageService;
import cn.sciento.farm.devicemanager.service.DevInsectSituationLampHistoryService;
import cn.sciento.farm.devicemanager.service.DevInsectSituationLampService;
import cn.sciento.fluorite.api.device.manage.CapturePicApi;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.Arrays;
import java.util.List;

/**
 * @author Walton
 * @since 2020/8/19 11:48
 */
@Slf4j
@Service(value = MqttConstant.PROTOCOL_PREFIX + MqttConstant.PROTOCOL_LAMP_INSECT_SITUATION)
public class InsectSituationLampDataAdapter extends AbstractStringAdapter {

    @Autowired
    private List<InsectSituationLampDisposeService>  insectLampDisposeServiceList;

    @Override
    public void analyseStringData(String[] subTopic, String message) {
        log.info("虫情灯topic {}  消息 {}", Arrays.toString(subTopic),message);
        message = message.replace("id", "modbusAddr");
        String type = subTopic[1];
        String eui = subTopic[2];
        switch (type) {
            case MqttConstant.UP:
                InsectLampSwitch insectLampSw = JSON.parseObject(message, InsectLampSwitch.class);
                InsectLampParam insectLampParam = JSON.parseObject(message, InsectLampParam.class);
                if (insectLampSw.getLighthouseSwitch() != null || insectLampSw.getHeatSwitch() != null
                    || insectLampSw.getTileSwitch() != null || insectLampSw.getLightSwitch() != null
                    || insectLampSw.getUpDoorSwitch() != null || insectLampSw.getLowDoorSwitch() != null
                    || insectLampSw.getPhtotsSwitch() != null || insectLampSw.getTimeSwitch() != null
                    || insectLampSw.getSweepSwitch() != null || insectLampSw.getStatusRefresh() != null
                ) {
                    insectLampSw.setEui(eui);
                    for (InsectSituationLampDisposeService insectLampDisposeService : insectLampDisposeServiceList) {
                        insectLampDisposeService.changeSwitch(insectLampSw);
                    }
                } else {
                    insectLampParam.setEui(eui);
                    for (InsectSituationLampDisposeService insectLampDisposeService : insectLampDisposeServiceList) {
                        insectLampDisposeService.changeParam(insectLampParam);
                    }
                }
                break;
            case MqttConstant.DOWN:
                break;
            default:
                break;
        }
    }

}
